import { MutationTree } from "vuex";
import storage from "store";
import { MutationTypes } from "./mutation-types";
import { SysState } from "./types";

export type Mutations<S = SysState> = {
  [MutationTypes.SET_SYS_INFO](state: S, payload: any[]): void;
  [MutationTypes.SET_SYS_DB](state: S, payload: any[]): void;
  [MutationTypes.SET_VERSION](state: S): void;
  [MutationTypes.CLEAR_SYS_INFO](state: S, payload: number): void;
};

export const sysMutations: MutationTree<SysState> & Mutations = {
  [MutationTypes.SET_SYS_INFO](state, payload) {
    state.sysInfo = payload;
    // storage.set(MutationTypes.SET_SYS_INFO, payload);
  },
  [MutationTypes.CLEAR_SYS_INFO](state, payload) {
    if (payload === -1) {
      state.sysInfo = [];
    } else {
      state.sysInfo = state.sysInfo.filter(m => m.source !== payload);
    }
  },
  [MutationTypes.SET_SYS_DB](state, payload) {
    state.indexedDB = payload;
  },
  [MutationTypes.SET_VERSION](state) {
    const storeData = state.version;
    storage.set(MutationTypes.SET_VERSION, storeData);
  }
};
